<template>
	<div class="">
    <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item >同步处理列表</el-breadcrumb-item>
    </el-breadcrumb>
		<data-list class="width-ctl-btn" :filters="filters" @data-refresh="handleRefresh" :total="total" ref="dataList">
      <div class="table-fn-btn">
        <el-button @click="handleRebuildList" type="primary" size="small">批量重新生成</el-button>
        <el-button @click="handleResignList" type="primary" size="small">批量重新签章</el-button>
      </div>
      <el-table
        :data="tableData"
				@select="selectRow"
				@select-all="selectAll"
        style="width: 100%">
					<el-table-column
						align="center"
						type="selection"
					>
					</el-table-column>
          <el-table-column
						align="center"
            type="index"
            label="序号"
            width="80">
          </el-table-column>
          <el-table-column
						min-width="310"
						align="center"
            prop="batchNumber"
            label="批次号">
            <template scope="scope">
							<el-tooltip class="of-dot" effect="dark" :content="scope.row.batchNumber" placement="top">
								<span>{{scope.row.batchNumber}}</span>
							</el-tooltip>
						</template>
          </el-table-column>
          <el-table-column
						align="center"
            prop="relationNumber"
						width="160"
            label="关联编号">
          </el-table-column>
          <el-table-column
						align="center"
						min-width="180"
            prop="orderNumber"
            label="订单编号">
            <template scope="scope">
							<el-tooltip class="of-dot" effect="dark" :content="scope.row.orderNumber" placement="top">
								<span>{{scope.row.orderNumber}}</span>
							</el-tooltip>
						</template>
          </el-table-column>
          <el-table-column
						align="center"
						min-width="280"
            prop="contractName"
            label="模板编号/名称">
            <template scope="scope">
							<el-tooltip class="of-dot" effect="dark" :content="scope.row.contractName" placement="top">
								<span>{{scope.row.contractName}}</span>
							</el-tooltip>
						</template>
          </el-table-column>
          <el-table-column
						align="center"
						min-width="180"
            prop="createTime"
            label="处理申请时间">
          </el-table-column>
          <el-table-column
						align="center"
						min-width="180"
            prop="finishTime"
            label="处理完成时间">
          </el-table-column>
          <el-table-column
						min-width="150"
						align="center"
            label="合同协议生成状态">
            <template scope="scope">
              {{translate.contractPdfStatus[scope.row.contractPdfStatus]}}
            </template>
          </el-table-column>
          <el-table-column
						min-width="130"
						align="center"
            label="签章状态">
            <template scope="scope">
              {{translate.contractCaStatus[scope.row.contractCaStatus]}}
            </template>
          </el-table-column>
          <el-table-column
						min-width="100"
						align="center"
            prop="caChannel"
            label="签章通道">
          </el-table-column>
          <el-table-column
						min-width="200"
						align="center"
            prop="caResult"
            label="错误反馈">
            <template scope="scope">
							<el-tooltip class="of-dot" effect="dark" :content="scope.row.pdfResult+'，'+scope.row.caResult" placement="top">
								<span>{{scope.row.pdfResult}}，{{scope.row.caResult}}</span>
							</el-tooltip>
						</template>
          </el-table-column>
         <el-table-column label="操作" align="center" fixed="right" width="260">
            <template scope="scope">
							<div class="of-hidden actList">
								<el-button @click="handleRebuild([scope.row])" type="primary" size="small">重新生成</el-button>
								<el-button @click="handleResign([scope.row])" type="primary" size="small">重新签章</el-button>
								<el-button type="primary" size="small" @click="download(scope.row.contractInfoId)">下载合同</el-button>
							</div>
            </template>
          </el-table-column>
      </el-table>
    </data-list>
	</div>
</template>

<script>
import api from '../../api'
import * as types from '../../store/types'
import translate from '../../translate'
import { baseURL,baseURLDownload } from '../../api/config.js'

var contractPdfStatus=[],contractCaStatus=[]
for(let prop in translate.contractPdfStatus){
	contractPdfStatus.push({
		label:translate.contractPdfStatus[prop],
		value:prop
	})
}
for(let prop in translate.contractCaStatus){
	contractCaStatus.push({
		label:translate.contractCaStatus[prop],
		value:prop
	})
}
export default {
  name: 'hello',
  data () {
    return {
			contractPdfStatus:contractPdfStatus,//合同协议生成状态
			contractCaStatus:contractCaStatus, //ca生成状态
			selectedContracts:[],//选中的行[object]
      translate:translate,
      vis_rebuildItem:false,
      filters:[
        {
          label:'批次号',
          type:'input',
          field:'batchNumber'
        },
        {
          label:'关联编号',
          type:'input',
          field:'relationNumber'
        },
        {
          label:'订单号',
          type:'input',
          field:'orderNumber'
        },
        {
          label:'模板编号',
          type:'input',
          field:'contractEnum'
        },
        {
          label:'处理申请时间',
          type:'date',
          field:'createTime'
        },
        {
          label:'PDF生成状态',
          type:'select',
          field:'contractPdfStatus',
          options:contractPdfStatus
        },
        {
          label:'ca签章状态',
          type:'select',
          field:'contractCaStatus',
          options:contractCaStatus
        }
      ],
      tableData: [],
      total:0,
      viewVisible:false
    }
  },
  mounted(){

  },
  methods:{
		download(id){
			window.location.href=this.downloadUrl(id);
		},
		resignBtnLoading(contracts,isLoading){
			contracts.forEach((item1)=>{
				this.tableData.forEach((item2)=>{
					if(item1.contractInfoId==item2.contractInfoId){
						item1.btnReSignLoading=isLoading;
					}
				})
			})
		},
		rebuildBtnLoading(contracts,isLoading){
			contracts.forEach((item1)=>{
				this.tableData.forEach((item2)=>{
					if(item1.contractInfoId==item2.contractInfoId){
						item1.btnReBuildLoading=isLoading;
					}
				})
			})
		},
		handleRebuildList(){
			if(this.selectedContracts.length>0){
				this.handleRebuild(this.selectedContracts)
			}
		},
		handleResignList(){
			if(this.selectedContracts.length>0){
				this.handleResign(this.selectedContracts)
			}
		},
		selectRow(selection){
			console.log(selection);
			this.selectedContracts=selection;
		},
		selectAll(selection){
			var selectedContracts=[];
			selection.forEach((item,index)=>{
				selectedContracts.push(item);
			})
			this.selectedContracts=selectedContracts;
		},
    downloadUrl(contractInfoId){
			return api.downloadContract(contractInfoId)
    },
    handleRefresh(params){
      console.log("handleRefresh @@@",params)
      params.pageNo=params.pageNum;
      delete params.pageNum;
			//请求同步处理列表接口
      api.syncList(params).then((res)=>{
        if(res.data.code.indexOf("SUCC")!=-1){
					res.data.resultJson.lists.forEach((item,index)=>{
						item.btnReBuildLoading=false
						item.btnReSignLoading=false
					})
          this.tableData=res.data.resultJson.lists;
          this.total=res.data.resultJson.count;
          console.log("this.tableData~~~~~",this.tableData)
        }else{
          this.$message({
            message: res.data.info,
            type: 'error'
          });
        }
      }).catch((err)=>{
        console.log(err)
      })
    },
		handleRebuild(contracts){
			var words1='',words2='';
			var selectedIds=[];
			contracts.forEach((item,index)=>{
				selectedIds.push(item.contractInfoId)
			});
			if(contracts.length>1){//多条
				words1="批量";
				words2="本次共选择" +contracts.length+ "份合同协议批量重新生成";
			}else{//单个
				words1='《'+contracts[0].contractName+'》';
				words2='重新生成合同《'+contracts[0].contractName+'》';
			}
			this.$confirm(words2, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				var params={contractInfoId:selectedIds.join(',')};
				this.rebuildBtnLoading(contracts,true);//按钮状态处理
				/*
				var loading= this.$message({
          showClose: true,
					iconClass:'fa loading',
					duration:0,
          message: words1+'同步生成请求中。。。'
        });
				*/
				this.$store.commit(types.LOADING_COUNT_ADD);
				api.reBuildContract(params).then((res)=>{
					//loading.close();//关闭生成中提示
					this.rebuildBtnLoading(contracts,false);//按钮状态处理
					if(res.data.code.indexOf("SUCC")!=-1){
						this.$message({
							type: 'success',
							message: res.data.info
						});
					}else{
						this.$message({
							type: 'error',
							message: res.data.info
						});
					}
				}).catch((err)=>{
					console.log(err);
				}).then(()=>{
					this.$store.commit(types.LOADING_COUNT_MINUS);
				})
			}).catch(() => {
			});
		},
		handleResign(contracts){
			var selectedIds=[],words1='',words2='';
			contracts.forEach((item,index)=>{
				selectedIds.push(item.contractInfoId)
			});
			if(contracts.length>1){
				words1="批量";
				words2="本次共选择"+contracts.length+"份合同协议重新签章";
			}else{
				words1='《'+contracts[0].contractName+'》';
				words2='重新签章合同《'+contracts[0].contractName+'》';
			}
			this.$confirm(words2, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.resignBtnLoading(contracts,true)//按钮状态处理
				var params={contractInfoId:selectedIds.join(',')};
				this.$store.commit(types.LOADING_COUNT_ADD);
				api.reBuildCA(params).then((res)=>{
					console.log(res);
					this.resignBtnLoading(contracts,false)//按钮状态处理
					if(res.data.code.indexOf("SUCC")!=-1){
						this.$message({
							type: 'success',
							message: res.data.info
						});
					}else{
						this.$message({
							type: 'error',
							message: res.data.info
						});
					}
				}).catch((err)=>{
					console.log(err);
				}).then(()=>{
					this.$store.commit(types.LOADING_COUNT_MINUS);
				})
			}).catch((err) => {
				console.log(err);
			});
		}
  }
}
</script>

<style scoped>
.breadcrumb{
  margin-top: 20px;
}
button.is-disabled a{
	color: #ccc;
}
</style>
